//
//  StudentDaoImpl.m
//  数据访问对象模式
//
//  Created by flannery on 2018/12/18.
//  Copyright © 2018年 flannery. All rights reserved.
//

#import "StudentDaoImpl.h"

@interface StudentDaoImpl ()
@property (nonatomic, strong) NSMutableArray *students;
@end

@implementation StudentDaoImpl

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.students = [[NSMutableArray alloc]init];
        Student *student1 = [[Student alloc] initWithName:@"Robert" rollNo:0];
        Student *student2 = [[Student alloc] initWithName:@"John" rollNo:1];
        [self.students addObject:student1];
        [self.students addObject:student2];
    }
    return self;
}

- (void)deleteStudent:(Student *)student {
    [self.students removeObject:student];
    NSLog(@"Student: roll No %d , deleted from database.", student.rollNo);
}

- (NSArray *)getAllStudents {
    //self.students removeObject:
    return self.students;
}

- (Student *)getStudent:(int)rollNo {
    if(rollNo <= [self.students count]) {
        return [self.students objectAtIndex:rollNo];
    }
    return nil;
}

- (void)updateStudent:(Student *)student {
    [[self.students objectAtIndex:student.rollNo] setName:student.name];
    NSLog(@"Student: Roll No %d ,updated in the database.", student.rollNo);
}

@end
